<?php
/* Start session and load lib */
require_once('../model.php');
require_once('../geo_code_proxy.php');
require_once('../util.php');
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

function tweet_log($str) {
  $file = fopen('.tweet_log', 'a');
  fwrite($file, $str, strlen($str));
  fclose($file);
}

function get_last_id() {
  $file = fopen(".latest_tweet_id", "r");
  $id = fgets($file);

  if (!$id) {
    fclose($file);
    return false;
  }
  fclose($file);
  return trim($id);
}

function set_last_id($id) {
  $file = fopen(".latest_tweet_id", "r+");
  if (!fwrite($file, $id . "\n"))
    return false;
  fclose($file);
  return true;
}

function is_report($subject) {
  return (strstr($subject, "#제보") != false);
}

function get_hash_tags($subject) {
  preg_match_all("/#([^ ]+)/", $subject, &$out, PREG_SET_ORDER);
  $ret = array();
  foreach ($out as $match) {
    $ret[] = $match[1];
  }
  return $ret;
}

function fetch_address($tags) {
  $ret = array();

  foreach($tags as $tag) {
    $lat_lng = geo_code($tag);

    if ($lat_lng == false) {
      continue;
    } else {
      $ret[] = $tag;
    }
  }
  return implode(" ", $ret);
}

function fetch_content($text) {
  $text = str_replace("@diycitykr", "", $text);
  //$text = preg_replace("/#([^ ]+)/", "", $text);
  $text = trim($text);
  return $text;
}

function test_oauth() {
  $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET);
  $content = $connection->get('statuses/mentions');
	print_r($content); 
}

function update_tweet_datas() {
  $last_id = get_last_id();
  $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET);
  $content = $connection->get('statuses/mentions');
  $recent_id = $content[0]->id;

  $ret = array();
  if ($last_id == $recent_id) { /*END*/
    return;
  }

  foreach($content as $item) {
    if ($last_id >= $item->id)
      break;

    $problem = new DiyModel('problems');

    if(is_report($item->text)) {
      $tw = array();

      $text = str_replace("#제보", "", $item->text);
      $address = fetch_address(get_hash_tags($text));
      $geo = geo_code($address);

      $text = fetch_content($text);

      if ($geo == false) {
	$tw['state'] = -1;
      }

      $address = coord_to_address($geo[0], $geo[1]);
      $address_obj = simplexml_load_string($address);
      $name1 = (string)$address_obj['name1'];
      $name2 = (string)$address_obj['name2'];
      $name3 = (string)$address_obj['name3'];

      $tw['lat'] = $geo[0];
      $tw['lng'] = $geo[1];
      $tw['address_hash'] = get_address_hash($name1, $name2, $name3);
      $tw['address1'] = $name1;
      $tw['address2'] = $name2;
      $tw['address3'] = $name3;

      $time = get_current_time();
      $tw['created_time'] = $time;
      $tw['modified_time'] = $time;
      $tw['email'] = '@' . $item->user->screen_name;
      $tw['reporter'] = $item->user->name;
      $tw['details'] = $text;
      $tw['subject'] = shorten_title($text);
      $tw['category'] = '기타';
      $tw['service_type'] = 'twitter';
      
      $log = print_r($tw, true);
      tweet_log($log);
      $problem->save($tw);
    }
  }
  set_last_id($recent_id);
}

update_tweet_datas();
//test_oauth();
?>
